#ifndef MESSAGESHOWAREA_H
#define MESSAGESHOWAREA_H

#pragma once
#include"./model/data.h"
#include <QObject>
#include <QScrollArea>
#include<QVBoxLayout>
#include<QTimer>
#include<QScrollBar>
#include<QPushButton>
#include<QLabel>
#include<QPainter>
#include<QPainterPath>
#include<QEvent>

/////////////////////////////
///消息展示区
/////////////////////////////
class MessageItem : public QWidget
{
     Q_OBJECT
public:
     MessageItem(bool isLeft);

    // 通过⼯⼚设计模式构造 Message
    static MessageItem* makeMessageItem(bool isLeft, const model::Message& message);

private:

    static QWidget* makeTextMessageItem(const QByteArray& content, bool isLeft);
    static QWidget* makeImageMessageItem(const QString& fileId, const QByteArray& content, bool isLeft);
    static QWidget* makeFileMessageItem(const QString& fileId, const QByteArray& content, bool isLeft, const QString& fileName);
    static QWidget* makeSoundMessageItem(const QString& fileId, const QByteArray& content, bool isLeft);

    bool isLeft; //判断消息是左边还是右边

};

class MessageShowArea : public QScrollArea
{
public:
    MessageShowArea(QWidget* parent = nullptr);

    // 新增⼀个 Message (尾插)
    void addMessage(bool isLeft, const model::Message& message);
    // 新增⼀个 Message (头插)
    void addFrontMessage(bool isLeft, const model::Message& message);

    // 清空所有 Message
    void clear();

    // 设置滚动条滚动到末尾
    void scrollToEndLater();

    bool eventFilter(QObject* watched, QEvent* event)override;
private:
    QWidget* container;
};

class MessageContentLabel : public QWidget{
    Q_OBJECT
public:
    MessageContentLabel(model::MessageType type, const QString& text, const QString&fileId, const QByteArray& content, bool isLeft);

    void paintEvent(QPaintEvent* ) override;
    //void mousePressEvent(QMouseEvent * ) override;
    //void contextMenuEvent(QContextMenuEvent* event) override;

private:
    QLabel* label;
    // 保存消息类型
    model::MessageType messageType;
    // ⽂件id
    QString fileId;
    // 保存消息内容(⽐如⽂件消息, 语⾳消息的⼆进制内容), 为后续的 "播放" / "另存为" 做准备
    QByteArray content;
    // 是否是左侧消息
    bool isLeft;
};

class MessageImageLabel : public QWidget{
    Q_OBJECT
public:
    MessageImageLabel(const QString &fileId, const QByteArray &content, bool isLeft);
private:
    QString fileId;
    QByteArray content;
    bool isLeft;
};

#endif // MESSAGESHOWAREA_H
